﻿using ChoresAPI.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using ChoresAPI.Data.Models;
using ChoresAPI.Models;
using Microsoft.AspNet.Identity;

namespace ChoresAPI.Controllers
{
    [Authorize]
    public class ChoresController : ApiController
    {
        [Route("todos")]
        public IHttpActionResult GetChores()
        {
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                List<ChoreIndexVm> cVm = new List<ChoreIndexVm>();

                cVm = db.Chores.Select(c => new ChoreIndexVm
                {
                    ChoreID = c.ChoreID,
                    Title = c.Title,
                    UserID = c.UserID
                }).ToList();

                return Ok(cVm);
            }
        }

        [Route("GetInfo")]
        public IHttpActionResult GetInfo()
        {
            string userType = "General";

            if (User.IsInRole("Admin"))
            {
                userType = "Admin";
            }
            UserInfoVm uVm = new UserInfoVm();

            uVm.userType = userType;
            uVm.Name = User.Identity.Name;

            return Ok(uVm);
        }

        [Route("UserChores/{page}")]
        public IHttpActionResult GetUserChores(int page)
        {
            string uId = User.Identity.GetUserId();

            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                List<ChoreIndexVm> cVm = new List<ChoreIndexVm>();

                cVm = db.Chores.Where(u => u.UserID == uId).Select(c => new ChoreIndexVm
                {
                    ChoreID = c.ChoreID,
                    Title = c.Title,
                    UserID = c.UserID
                }).OrderBy(o => o.ChoreID).Skip(page).Take(5).ToList();

                return Ok(cVm);
            }
        }
    }
}
